package com.pshcp.service;

import com.framework.util.SystemUtil;
import com.pshcp.domain.dto.DoctorWorkCalendarMemoDTO;
import com.pshcp.domain.model.DoctorWorkCalendarMemo;
import com.pshcp.mapper.DoctorWorkCalendarMemoMapper;

import com.github.pagehelper.PageHelper;
import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.pshcp.common.enums.CommonStatusEnum;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工作日历-日程 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorWorkCalendarMemoService {

    @Autowired
    private DoctorWorkCalendarMemoMapper doctorWorkCalendarMemoMapper;

    public List<DoctorWorkCalendarMemo> list(Long doctorId,String startTime,String endTime, Integer type) {
        Example example = new Example(DoctorWorkCalendarMemo.class);
        Example.Criteria criteria = example.createCriteria();
        if(type != null){
            criteria.andEqualTo("type", type);
        }
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andGreaterThanOrEqualTo("time", startTime+" 00:00:00");
        criteria.andLessThanOrEqualTo("time", endTime+" 23:59:59");
        example.setOrderByClause(" created_time desc");
        List<DoctorWorkCalendarMemo> list = doctorWorkCalendarMemoMapper.selectByExample(example);
        return list;
    }

    public PageList<DoctorWorkCalendarMemo> list(DoctorWorkCalendarMemoDTO doctorWorkCalendarMemoDTO) {
        if (doctorWorkCalendarMemoDTO.getPageNum() != null && doctorWorkCalendarMemoDTO.getPageSize() != null) {
            PageHelper.startPage(doctorWorkCalendarMemoDTO.getPageNum(), doctorWorkCalendarMemoDTO.getPageSize());
        }
        Example example = new Example(DoctorWorkCalendarMemo.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorWorkCalendarMemoDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorWorkCalendarMemoDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorWorkCalendarMemo> list = doctorWorkCalendarMemoMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorWorkCalendarMemo> select(DoctorWorkCalendarMemo doctorWorkCalendarMemo) {
        return doctorWorkCalendarMemoMapper.select(doctorWorkCalendarMemo);
    }

    public int insert(DoctorWorkCalendarMemo doctorWorkCalendarMemo) {
        doctorWorkCalendarMemo.setId(SystemUtil.getId());
        doctorWorkCalendarMemo.setStatus(CommonStatusEnum.INIT.getCode());
        doctorWorkCalendarMemo.setCreatedTime(new Date());
        return doctorWorkCalendarMemoMapper.insert(doctorWorkCalendarMemo);
    }

    public int update(DoctorWorkCalendarMemo doctorWorkCalendarMemo) {
        doctorWorkCalendarMemo.setUpdateTime(new Date());
        return doctorWorkCalendarMemoMapper.updateByPrimaryKeySelective(doctorWorkCalendarMemo);
    }

    public DoctorWorkCalendarMemo findById(Long id) {
        return doctorWorkCalendarMemoMapper.selectByPrimaryKey(id);
    }

    public List<DoctorWorkCalendarMemo> findByIds(List<Long> ids) {
        Example example = new Example(DoctorWorkCalendarMemo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorWorkCalendarMemoMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorWorkCalendarMemo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorWorkCalendarMemo doctorWorkCalendarMemo = new DoctorWorkCalendarMemo();
        doctorWorkCalendarMemo.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorWorkCalendarMemoMapper.updateByExampleSelective(doctorWorkCalendarMemo, example);
    }

}
